<?php
/**
 * Created by PhpStorm.
 * User: Hk
 * Date: 2021/1/26
 * Time: 18:04
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Community;
use App\Models\Area;
use App\Models\Property;
use App\Services\AreaService;
use Illuminate\Http\Request;

class CommunityController extends Controller
{
    public $model;

    public function __construct()
    {
        $this->model = new Community();
    }

    /**
     * Notes: 小区列表
     * User: HK
     * Date: 2021/1/26
     * Time: 18:05
     * email: 1919194843@qq.com
     */
    public function index(Request $request)
    {
        $param = $request->query();
        $where[] = ['status', '>', 0];
        if (isset($param['community_name']) && !empty($param['community_name'])) $where[] = ['community_name', 'like', "%{$param['community_name']}%"];
        if (isset($param['areas']) && !empty($param['areas'])) {
            [$province, $city, $area] = $param['areas'];
            $where[] = ['area', '=', $area];
        }

        $lists = $this->model->getPaginate($where, 'community_id', 'desc');

        foreach ($lists['data'] as $key => $val) {
            $lists['data'][$key]['property_name'] = (new Property())->getValueById($val['property_id'], 'name');
        }
        return $this->success($lists);
    }

    /**
     * Notes: 获取省市区三级联动数据接口
     * User: HK
     * Date: 2021/1/27
     * Time: 9:39
     * email: 1919194843@qq.com
     */
    public function area_lists(AreaService $area_service)
    {
        $list = $area_service->getAreas()['data'];
        return $this->success($list);
    }

    /**
     * Notes: 新增小区
     * User: HK
     * Date: 2021/1/27
     * Time: 10:31
     * email: 1919194843@qq.com
     */
    public function store(Request $request)
    {
        return $this->model->create($request->post()) ? $this->success('添加成功!') : $this->error('添加失败!');
    }

    /**
     * Notes: 单个小区详情
     * User: HK
     * Date: 2021/1/27
     * Time: 11:15
     * email: 1919194843@qq.com
     */
    public function show($id)
    {
        return $this->success($this->model->getInfo(['community_id' => $id]));
    }

    /**
     * Notes: 修改小区
     * User: HK
     * Date: 2021/1/27
     * Time: 11:33
     * email: 1919194843@qq.com
     */
    public function update(Request $request)
    {
        return $this->model->edit($request->post()) ? $this->success('修改成功!') : $this->error('修改失败!');
    }

    /**
     * Notes: 删除
     * User: HK
     * Date: 2021/1/27
     * Time: 15:23
     * email: 1919194843@qq.com
     * @param $ids
     * @return array
     */
    public function destroy($ids)
    {
        return $this->model->deletes($ids) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * Notes: 修改状态
     * User: HK
     * Date: 2021/1/27
     * Time: 15:23
     * email: 1919194843@qq.com
     * @param Request $request
     */
    public function edit_status(Request $request)
    {
        $community_id = $request->post('community_id');
        $status = $this->model->getValue(['community_id' => $community_id], 'status') == 1 ? 2 : 1;
        return $this->model->updateByWhere(['community_id' => $community_id], ['status' => $status]) ? $this->success('修改成功!') : $this->error('修改失败!');
    }
}
